<template>
  <view class="fitting-list">
    <scroll-view scroll-y @scrolltolower="loadMore">
      <view class="empty" v-if="isEmpty">
        <text>暂无数据</text>
      </view>
      <uni-list v-if="!isEmpty">
        <uni-list-item clickable :title="data.parts_name" :class="{ active: selectedData && selectedData.parts_code === data.parts_code && selectedData.parts_model === data.parts_model }" v-for="(data, index) in dataList" :key="index" @click="dataSelect(data)">
          <view slot="body">
            <view class="name">{{data.parts_name}}</view>
            <view class="type">{{data.parts_model}}</view>
          </view>
        </uni-list-item>
      </uni-list>
    </scroll-view>
  </view>
</template>

<script>
  import { getFittingDataList } from '../../api/device/fitting';
  
  export default {
    name: 'FittingList',
    components: {},
    props: {
      storeInfo: {
        type: Object,
        default() {
          return {};
        },
      },
    },
    data() {
      return {
        isEmpty: false,
        dataList: [],
        isFinish: false,
        pageData: {
          pageNum: 1,
          pageSize: 20,
        },
        selectedData: null,
      };
    },
    watch: {
      selectedData: {
        handler(value) {
          this.$emit('select-fitting-change', { data: value });
        },
      },
    },
    mounted() {
    },
    created() {
      this.getData();
    },
    methods: {
      async getData() {
        const param = { ...this.pageData, wCode: this.storeInfo.wCode };
        const res = await getFittingDataList(param);
        if (res.code === 200) {
          const dataList = res.rows || [];
          this.dataList = [ ...this.dataList, ...dataList ];
          this.isEmpty = this.dataList.length < 1;
          this.isFinish = res.total === this.dataList.length;
        }
      },
      dataSelect(data) {
        console.log(data);
        const { selectedData } = this;
        if (selectedData && data.parts_code === selectedData.parts_code && data.parts_model === selectedData.parts_model) {
          this.selectedData = null;
          return;
        }
        this.selectedData = data;
      },
      loadMore() {
        if (this.isFinish) {
          return;
        }
        this.pageData.pageNum += 1;
        this.getData();
      },
    },
  };
</script>

<style lang="scss" scoped>
  .fitting-list {
    &, * {
      box-sizing: border-box;
    }
    height: 100%;
    overflow: hidden;
    uni-scroll-view {
      height: 100%;
      
      .empty {
        text-align: center;
        color: #cccccc;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      
      /deep/ .uni-list-item {
        .name{
          font-size: 14px;
          color: black;
          line-height: 1.5;
        }
        .type{
          font-size: 14px;
          color: #888888;
          margin-top: 5px;
        }
        &.active {
          background-color: #007aff;
          .name,
          .type{
            color: white;
          }
          .uni-list-item__content {
            .uni-list-item__content-title {
              color: white;
            }
          }
        }
      }
      
      /deep/ .uni-scroll-view-refresher {
        background-color: #f1f1f1 !important;
      }
      
      /deep/ .uni-swipe {
        border-bottom: 1px solid #f1f1f1;
      }
    }
  }
</style>
